package com.example.login.activity

import android.os.Bundle
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.example.activity.BaseActivity

import com.example.common.User
import com.example.login.databinding.AcRegistBinding

import com.example.login.factory.ViewModelFactory
import com.example.login.model.LoginState
import com.example.login.viewmodel.LGViewModel
import com.kongzue.dialogx.dialogs.MessageDialog
import com.kongzue.dialogx.dialogs.TipDialog
import com.kongzue.dialogx.dialogs.WaitDialog

@Route(path ="/login/regist")
class LoginRegistActivity: BaseActivity() {
    private lateinit var lgbind : AcRegistBinding
    private lateinit var lgVM:LGViewModel
    private var name: String? = null;
    private lateinit var m_user: User
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        lgbind = AcRegistBinding.inflate(layoutInflater)
        setContentView(lgbind.root)
        lgVM = ViewModelProvider(this,ViewModelFactory())
            .get(LGViewModel::class.java)
        lgbind.ivBack.setOnClickListener {
            finish()
        }
        lgbind.toolbar.setNavigationOnClickListener {
            finish()
        }
        lgbind.next.setOnClickListener {
            name = lgbind.userName.text.toString()
            var pw1 = lgbind.userPw.text.toString()
            var pw2 = lgbind.edPw2.text.toString()
            if(pw1.isEmpty()||pw2.isEmpty()|| pw1 != pw2){
                MessageDialog.show("提示","密码不相等或者为空","确定")
                return@setOnClickListener
            }
            name?.let {
                WaitDialog.show("正在注册...")
                lgVM.regist(name!!,lgbind.edPw2.text.toString())
                m_user = User()
                m_user.ps=lgbind.edPw2.text.toString()
                m_user.name=name
                return@setOnClickListener
            }
        }
        lgVM.loginResult.observe(this, Observer {
            if(it.type == LoginState._LoginState.REGISTSUCCESS){
                ARouter.getInstance().build("/app/home").navigation();
                finish()
            }else{
                TipDialog.show(it.msg)
              //  DialogXK.show(it.msg,DialogXK.TYPE.ERROR)
            }
        })
    }

}